﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Bifrost.WcfSvr;
using Bifrost;
using CLIENT_EMR.Doctor_Bll.Patient_Action_Manager;

namespace CLIENT_EMR.Doctor_Bll
{
    public partial class ucInpatients : UserControl
    {

        public DevComponents.DotNetBar.TabControl tabControl_Patient = null; //副窗体的页控件
        private InPatientInfo CurrentInPatientinfo;     //当前病人实体

        public ucInpatients()
        {
            InitializeComponent();
        }

        private void frmInpatients_Load(object sender, EventArgs e)
        {
            try
            {
                RefData();
                frmAddPatientShow fc = new frmAddPatientShow();
                fc.TopMost = true;
                fc.Show();
            }
            catch
            { }
        }

        private void RefData()
        {            
            DataSet ds = App.wc.GetInptientBySectionCode(App.UserAccount.CurrentSelectRole.Section_Id);
            datagridViewx.DataSource = ds.Tables[0].DefaultView;
            datagridViewx.AutoResizeColumns();
        }

        private void datagridViewx_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string id = datagridViewx["主键", e.RowIndex].Value.ToString();
                //string pname = datagridViewx["病人姓名", e.RowIndex].Value.ToString();
                //string bed = datagridViewx["床位", e.RowIndex].Value.ToString();

                CurrentInPatientinfo = App.wc.InitPatient(id); //初始化病人实体


                //验证TabControl是否有重复
                if (tabControl_Patient != null)
                {
                    for (int i = 0; i < tabControl_Patient.Tabs.Count; i++)
                    {
                        if (id.ToString() == tabControl_Patient.Tabs[i].Name)
                        {
                            tabControl_Patient.SelectedTabIndex = i;
                            return;
                        }
                    }
                }


                TabControlPanel tabctpnDoc = new TabControlPanel();
                tabctpnDoc.AutoScroll = true;
                TabItem pageDoc = new TabItem();
                pageDoc.Name = id;
                pageDoc.Text = CurrentInPatientinfo.Sick_Bed_Name + " " + CurrentInPatientinfo.Patient_Name;
                pageDoc.Click += new EventHandler(page_Click);
                pageDoc.Tag = id;

                ucDocOperater fm = new ucDocOperater(CurrentInPatientinfo);
                fm.Dock = DockStyle.Fill;
                tabctpnDoc.Controls.Add(fm);
                tabctpnDoc.Dock = DockStyle.Fill;
                pageDoc.AttachedControl = tabctpnDoc;
                tabControl_Patient.Controls.Add(tabctpnDoc);
                tabControl_Patient.Tabs.Add(pageDoc);
                tabControl_Patient.Refresh();
                tabControl_Patient.SelectedTab = pageDoc;
            }
            catch(Exception ex)
            {
                App.MsgErr("请选择要操作的病人,具体错误："+ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void page_Click(object sender, EventArgs e)
        {
            if (tabControl_Patient.Tabs.Count > 0)
            {
                this.tabControl_Patient.AutoCloseTabs = false;
                TabItem item = (TabItem)sender;
                //Point mp = Cursor.Position;
                MouseEventArgs mp = (MouseEventArgs)e;
                Point pTab = item.CloseButtonBounds.Location;
                if (mp.X >= pTab.X && mp.X <= pTab.X + item.CloseButtonBounds.Width && mp.Y >= pTab.Y &&
                    mp.Y <= pTab.Y + item.CloseButtonBounds.Height)
                {

                    if (App.Ask("是否关闭当前病人的文书？"))
                    {
                        //App.ReleaseLockedDoc(item.Name);
                        this.tabControl_Patient.Tabs.Remove(item);

                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 新增病人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmPatientIN_Old fc = new frmPatientIN_Old("");
            fc.ShowDialog();
        }

        /// <summary>
        /// 右键操作菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridViewx_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (datagridViewx.Rows[e.RowIndex].Selected == false)
                    {
                        datagridViewx.ClearSelection();
                        datagridViewx.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (datagridViewx.SelectedRows.Count == 1)
                    {
                        datagridViewx.CurrentCell = datagridViewx.Rows[e.RowIndex].Cells[e.ColumnIndex];

                    }
                    //弹出操作菜单
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        /// <summary>
        /// 基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 病人基本信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {           
            string id=datagridViewx["主键", datagridViewx.CurrentRow.Index].Value.ToString();
            frmPatientIN_Old fc = new frmPatientIN_Old(id);
            fc.ShowDialog();
            
        }
    }
}
